﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-12-17 12:29:37
// 负责人：liucx
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;
namespace OF.Component.DAL
{
    #region  数据访问层
    /// <summary>
    /// 数据访问层
    /// </summary>	
    internal partial class MessagePool : IMessagePool
    {

        //连接字符串
        public virtual string ConnectionStringKey
        {
            get
            {
                return ConstantUtility.CGTConnectionString;
            }
        }
        #region GetMaxID



        public int GetMaxID()
        {
            int nReturn = 0;
            string szSql = @"SELECT ISNULL(MAX(MessagePoolID),0)+1
                               FROM [MessagePool]";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(szSql);
            try
            {
                object o = db.ExecuteScalar(dbc);
                if (o != null)
                {
                    nReturn = Convert.ToInt32(o);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return nReturn;
        }



        #endregion

        #region Basic CRUD functions
        private MessagePoolInfo GetByReader(IDataReader dr)
        {
            MessagePoolInfo info = new MessagePoolInfo();
            object obj;
            obj = dr["MessagePoolID"];
            if (obj != null && obj != DBNull.Value)
            {
                info.MessagePoolID = (long)obj;
            }
            obj = dr["UserID"];
            if (obj != null && obj != DBNull.Value)
            {
                info.UserID = (long)obj;
            }
            obj = dr["IsRead"];
            if (obj != null && obj != DBNull.Value)
            {
                info.IsRead = Convert.ToInt32(obj);
            }
            obj = dr["MessageStatus"];
            if (obj != null && obj != DBNull.Value)
            {
                info.MessageStatus = Convert.ToInt32(obj);
            }
            obj = dr["SendDate"];
            if (obj != null && obj != DBNull.Value)
            {
                info.SendDate = Convert.ToDateTime(obj);
            }
            obj = dr["ErrorMessage"];
            if (obj != null && obj != DBNull.Value)
            {
                info.ErrorMessage = obj.ToString();
            }
            obj = dr["MessageID"];
            if (obj != null && obj != DBNull.Value)
            {
                info.MessageID = (long)obj;
            }
            obj = dr["MessageType"];
            if (obj != null && obj != DBNull.Value)
            {
                info.MessageType = Convert.ToInt32(obj);
            }
            obj = dr["IsActive"];
            if (obj != null && obj != DBNull.Value)
            {
                info.IsActive = Convert.ToInt32(obj);
            }
            return info;
        }
        public long Insert(MessagePoolInfo info)
        {


            string strSql = @"INSERT INTO MessagePool			
			(			
			[UserID],
			[IsRead],
			[MessageStatus],
			[SendDate],
			[ErrorMessage],
			[MessageID],
			[MessageType],
			[IsActive]
			)
			 VALUES
			(			
			@UserID,
			@IsRead,
			@MessageStatus,
			@SendDate,
			@ErrorMessage,
			@MessageID,
			@MessageType,
			@IsActive
			)
			 SET @MessagePoolID=@@IDENTITY
			";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql);
            db.AddOutParameter(dbc, "@MessagePoolID", DbType.Int64, sizeof(Int64));
            db.AddInParameter(dbc, "@UserID", DbType.Int64, info.UserID);
            db.AddInParameter(dbc, "@IsRead", DbType.Int32, info.IsRead);
            db.AddInParameter(dbc, "@MessageStatus", DbType.Int32, info.MessageStatus);
            db.AddInParameter(dbc, "@SendDate", DbType.DateTime, info.SendDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@ErrorMessage", DbType.String, info.ErrorMessage);
            db.AddInParameter(dbc, "@MessageID", DbType.Int64, info.MessageID);
            db.AddInParameter(dbc, "@MessageType", DbType.Int32, info.MessageType);
            db.AddInParameter(dbc, "@IsActive", DbType.Int32, info.IsActive);


            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {
                throw;
            }
            return (long)db.GetParameterValue(dbc, "MessagePoolID");

        }


        public void Update(MessagePoolInfo info)
        {
            StringBuilder sbSql = new StringBuilder("UPDATE MessagePool SET ");
            sbSql.AppendLine("[UserID] = @UserID,");

            sbSql.AppendLine("[IsRead] = @IsRead,");

            sbSql.AppendLine("[MessageStatus] = @MessageStatus,");

            sbSql.AppendLine("[SendDate] = @SendDate,");

            sbSql.AppendLine("[ErrorMessage] = @ErrorMessage,");

            sbSql.AppendLine("[MessageID] = @MessageID,");

            sbSql.AppendLine("[MessageType] = @MessageType,");

            sbSql.AppendLine("[IsActive] = @IsActive");

            sbSql.Append(" where  [MessagePoolID] = @MessagePoolID");
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(sbSql.ToString());
            db.AddInParameter(dbc, "@MessagePoolID", DbType.Int64, info.MessagePoolID);
            db.AddInParameter(dbc, "@UserID", DbType.Int64, info.UserID);
            db.AddInParameter(dbc, "@IsRead", DbType.Int32, info.IsRead);
            db.AddInParameter(dbc, "@MessageStatus", DbType.Int32, info.MessageStatus);
            db.AddInParameter(dbc, "@SendDate", DbType.DateTime, info.SendDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@ErrorMessage", DbType.String, info.ErrorMessage);
            db.AddInParameter(dbc, "@MessageID", DbType.Int64, info.MessageID);
            db.AddInParameter(dbc, "@MessageType", DbType.Int32, info.MessageType);
            db.AddInParameter(dbc, "@IsActive", DbType.Int32, info.IsActive);


            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {
                throw;
            }
        }

        public void Delete(long messagePoolID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM [MessagePool]  ");
            strSql.Append("WHERE [MessagePoolID]=@MessagePoolID ");

            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbc, "@MessagePoolID", DbType.Int64, messagePoolID);
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {
                throw;
            }


        }

        public IList<MessagePoolInfo> Search(out int pageCount, int pageIndex, int pageSize, string where, string orderField, bool isDesc)
        {
            IList<MessagePoolInfo> list = new List<MessagePoolInfo>();
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetStoredProcCommand("sp_OF_Page");
            //设置参数
            db.AddOutParameter(dbc, "@pageCount", DbType.Int32, 4);
            db.AddInParameter(dbc, "@pageIndex", DbType.Int32, pageIndex);
            db.AddInParameter(dbc, "@pageSize", DbType.Int32, pageSize);
            db.AddInParameter(dbc, "@strWhere", DbType.String, where);
            db.AddInParameter(dbc, "@fldName", DbType.String, orderField);
            db.AddInParameter(dbc, "@OrderType", DbType.Boolean, isDesc);
            db.AddInParameter(dbc, "@tblName", DbType.String, "MessagePool");
            db.AddInParameter(dbc, "@strGetFields", DbType.String, "*");
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                while (dr.Read())
                {
                    MessagePoolInfo info = GetByReader(dr);
                    list.Add(info);
                }
            }
            pageCount = (int)db.GetParameterValue(dbc, "@PageCount");
            return list;
        }


        public MessagePoolInfo GetByID(long messagePoolID)
        {
            Model.MessagePoolInfo info = null;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  * FROM [MessagePool] ");
            strSql.Append("WHERE [MessagePoolID]=@MessagePoolID ");

            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbc, "@MessagePoolID", DbType.Int64, messagePoolID);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info = GetByReader(dr);
                }
            }
            return info;
        }


        #endregion
    }
    #endregion
}